package com.eddy.controller;

import com.eddy.entity.DataDecrypt;
import com.eddy.entity.QueryInfo;
import com.eddy.entity.User;
import com.eddy.service.UserService;
import com.eddy.util.DataEncryptUtils;
import com.eddy.util.DataUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
public class UserController {
    private static Logger log = Logger.getLogger(LoginController.class);
    @Autowired
    private UserService userService;

    @RequestMapping("/queryUser")
    public String getUserList(@RequestBody QueryInfo queryInfo){
        try {
            //总条数
            int total = userService.getUserTotal(queryInfo.getQuery());
            //数据
            //pageStart = (pageNumber - 1) * pageSize
            int pageStart = (queryInfo.getPageNumber() - 1) * queryInfo.getPageSize();
            List<User> list = userService.getAllUser(queryInfo.getQuery(), pageStart, queryInfo.getPageSize());
            if(list != null) {
                HashMap<String,Object> map = new HashMap<String,Object>();
                map.put("total",total);
                map.put("data",list);
                return DataUtils.resultData(map, "OK");
            }
        }catch (Exception e){
            log.error("GetUserList Exception:",e);
        }
        return DataUtils.resultData("Error");
    }

    @RequestMapping("/updateState")
    public String updateState(@RequestBody DataDecrypt decrypt){
        try {
            //解密请求
            User user = DataEncryptUtils.decryptParameter(decrypt, User.class);
            int res = userService.updateState(user.getId(), user.getState());
            if(res > 0){
                return DataUtils.resultData("OK");
            }
        }catch (Exception e){
            log.error("Update State Exception:",e);
        }
        return DataUtils.resultData("Error");
    }

    @RequestMapping("/addUser")
    public String addUser(@RequestBody DataDecrypt decrypt){
        try {
            //解密请求
            User user = DataEncryptUtils.decryptParameter(decrypt, User.class);
            //SHA加密密码
            user.setPassword(DataEncryptUtils.shaEncrypt(user.getPassword()));
            int res = userService.addUser(user);
            if(res > 0){
                return DataUtils.resultData("OK");
            }
        }catch (Exception e){
            log.error("Add User Exception:",e);
        }
        return DataUtils.resultData("Error");
    }

    @RequestMapping("/delUser")
    public String delUser(@RequestBody DataDecrypt decrypt){
        try {
            //解密请求
            int id = Integer.parseInt(DataEncryptUtils.decryptParameter(decrypt));
            int res = userService.deleteUser(id);
            if(res > 0){
                return DataUtils.resultData("OK");
            }
        }catch (Exception e){
            log.error("Delete User Exception:",e);
        }
        return DataUtils.resultData("Error");
    }

    @RequestMapping("/getUser")
    public String getUser(@RequestBody DataDecrypt decrypt){
        try {
            int id = Integer.parseInt(DataEncryptUtils.decryptParameter(decrypt));
            //数据
            User user = userService.getUser(id);
            if(user != null) {
                return DataEncryptUtils.encryptData(user, "OK");
            }
        }catch (Exception e){
            log.error("Get User Exception:",e);
        }
        return DataUtils.resultData("Error");
    }

    @RequestMapping("/updateUser")
    public String updateUser(@RequestBody DataDecrypt decrypt){
        try {
            //解密请求
            User user = DataEncryptUtils.decryptParameter(decrypt, User.class);
            //判断是否修改了密码
            User updUser = userService.getUser(user.getId());
            if(!updUser.getPassword().equals(user.getPassword())){
                //SHA加密密码
                user.setPassword(DataEncryptUtils.shaEncrypt(user.getPassword()));
            }
            int res = userService.updateUser(user);
            if(res > 0){
                return DataUtils.resultData("OK");
            }
        }catch (Exception e){
            log.error("Update User Exception:",e);
        }
        return DataUtils.resultData("Error");
    }
}
